package com.rz.paas.test.customView

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.rz.paas.test.R


class VolumeDetectionView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attributeSet, defStyleAttr) {

    //第一圈的颜色
    private var mFirstColor = 0

    // 第二圈的颜色
    private var mSecondColor = 0

    //当前进度
    private var mCurrentCount = 3

    //每个块块间的间隙
    private var mSplitSize = 0

    //个数
    private var mCount = 0

    //画笔
    private val mPaint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG)
    }
    private val mSplitPaint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG)
    }


    init {
        val typeArray =
            context.obtainStyledAttributes(attributeSet, R.styleable.VolumeDetectionView)
        var indexCount = typeArray.indexCount
        for (index in 0 until indexCount) {
            val attr = typeArray.getIndex(index)
            when (attr) {
                R.styleable.VolumeDetectionView_firstColor ->
                    mFirstColor = typeArray.getColor(attr, Color.GREEN);

                R.styleable.VolumeDetectionView_secondColor ->
                    mSecondColor = typeArray.getColor(attr, Color.CYAN)

                R.styleable.VolumeDetectionView_dotCount ->
                    mCount = typeArray.getInt(attr, 20)

                R.styleable.VolumeDetectionView_splitSize ->
                    mSplitSize = typeArray.getInt(attr, 20)
            }
        }
        typeArray.recycle()

        mPaint.isAntiAlias = true
        mPaint.style = Paint.Style.FILL
        mPaint.color = mFirstColor


        mSplitPaint.isAntiAlias = true
        mSplitPaint.style = Paint.Style.FILL
        mSplitPaint.color = mSecondColor
    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawOval(canvas);
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    /**
     * 根据参数画出每个小块
     */
    private fun drawOval(canvas: Canvas) {
        /**
         * 根据需要画的个数以及间隙计算每个块块所占的比例*360
         */
        var layoutParams = getLayoutParams() as ConstraintLayout.LayoutParams
        var startPosition = 0f /*+ layoutParams.leftMargin*/ + paddingLeft
        var topPosition = 0f + layoutParams.topMargin + paddingTop
        var bottomPosition =
            height - layoutParams.topMargin - layoutParams.bottomMargin - paddingBottom - paddingTop

        val itemWidth = mSplitSize

        for (i in 0 until mCount) {
            val oval = RectF(
                startPosition,
                topPosition,
                startPosition + (itemWidth) * 1.0f,
                bottomPosition * 1.0f
            )
            startPosition += itemWidth * 2

            canvas.drawRoundRect(oval, itemWidth * 1.0f, itemWidth * 1.0f, mPaint)
            if (i < mCurrentCount) {
                canvas.drawRoundRect(oval, itemWidth * 1.0f, itemWidth * 1.0f, mSplitPaint)
            }

        }

    }


    fun setCurrentVolume(volume: Int) {
        mCurrentCount = volume
        postInvalidate()
    }
}